/*
 * DialogsDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import com.cute4j.swing.Button;
import com.cute4j.swing.ColorChooser;
import com.cute4j.swing.Component;
import com.cute4j.swing.Container;
import com.cute4j.swing.FileChooser;
import com.cute4j.swing.FontChooser;
import com.cute4j.swing.Label;
import com.cute4j.swing.OptionPane;
import com.cute4j.swing.ScrollPane;
import com.cute4j.swing.Separator;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.graphics.Color;
import com.cute4j.swing.graphics.Font;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;

/**
 * Demonstration of the Dialog classes.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class DialogsDemo extends DemoModule {

// Public Constructor. ---------------------------------------------------------

  /**
   * Constructs new DialogsDemo object.
   */
  public DialogsDemo() {
    super("Dialogs Demo", "Demonstration of the Dialog classes.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    FormLayout layout = new FormLayout(
                              "p:g, p, 8dlu, p, 8dlu, p, 8dlu, p, 8dlu, p, p:g",
                              "p, 6dlu, p, 6dlu, p, 6dlu, p, 12dlu, p," +
                              "6dlu, p, 12dlu, p, 6dlu, p, 12dlu, p, 6dlu, p");
    layout.setColumnGroups(new int[][] {{2, 4, 6, 8, 10}});
    final Container panel = new Container(layout);
    panel.setInsets(16, 16, 16, 16);
    CellConstraints cc = new CellConstraints();

    panel.add(new Separator("File Chooser"), cc.xyw(2, 1, 9));

    final PanelLabel openFileNameLabel = new PanelLabel();
    final Button openFileNameButton = new Button("Open File");
    openFileNameButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         openFileNameLabel.setText(FileChooser.getOpenFileName(panel));
       }
     });
    panel.add(openFileNameButton, cc.xy(2, 3));
    panel.add(openFileNameLabel, cc.xyw(4, 3, 7));

    final PanelLabel saveFileNameLabel = new PanelLabel();
    final Button saveFileNameButton = new Button("Save File");
    panel.add(saveFileNameButton, cc.xy(2, 5));
    panel.add(saveFileNameLabel, cc.xyw(4, 5, 7));
    saveFileNameButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         saveFileNameLabel.setText(FileChooser.getSaveFileName(panel));
       }
     });

     final PanelLabel selectDirectoryLabel = new PanelLabel();
     final Button selectDirectoryButton = new Button("Select Directory");
     panel.add(selectDirectoryButton, cc.xy(2, 7));
     panel.add(selectDirectoryLabel, cc.xyw(4, 7, 7));
     selectDirectoryButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          selectDirectoryLabel.setText(
              FileChooser.getExistingDirectoryName(panel,
                                                   "Select Directory", null));
        }
      });

    panel.add(new Separator("Color Chooser"), cc.xyw(2, 9, 9));

    final PanelLabel colorLabel = new PanelLabel();
    final Button colorButton = new Button("Select Color");
    panel.add(colorButton, cc.xy(2, 11));
    panel.add(colorLabel, cc.xyw(4, 11, 7));
    colorButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         colorLabel.setBackground(
             ColorChooser.getColor(panel, colorLabel.getBackground()));
       }
     });

     panel.add(new Separator("Font Chooser"), cc.xyw(2, 13, 9));

     final PanelLabel fontLabel = new PanelLabel(
                                "The quick brown fox jumps over the lazy dog");
     final Button fontButton = new Button("Select Font");
     panel.add(fontButton, cc.xy(2, 15));
     panel.add(fontLabel, cc.xyw(4, 15, 7));
     fontButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          fontLabel.setFont(FontChooser.getFont(panel, fontLabel.getFont()));
        }
      });

    panel.add(new Separator("Message Box"), cc.xyw(2, 17, 9));

    final String title = "Cute4j SwingSet";
    final Button aboutButton = new Button("About Message");
    panel.add(aboutButton, cc.xy(2, 19));
    aboutButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         OptionPane.showAboutMessage(panel, "About Message.", title);
       }
     });
     final Button infoButton = new Button("Info Message");
     panel.add(infoButton, cc.xy(4, 19));
     infoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          OptionPane.showInformationMessage(panel, "Info Message.", title);
        }
      });
      final Button warningButton = new Button("Warning Message");
      panel.add(warningButton, cc.xy(6, 19));
      warningButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           OptionPane.showWarningMessage(panel, "Warning Message!", title,
                                         OptionPane.SAVE_OPTION |
                                         OptionPane.DISCARD_OPTION);
         }
       });
       final Button questionButton = new Button("Question Message");
       panel.add(questionButton, cc.xy(8, 19));
       questionButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            OptionPane.showQuestionMessage(panel, "Question Message?", title,
                                           OptionPane.YES_NO_CANCEL_OPTIONS);
          }
        });
        final Button errorButton = new Button("Error Message");
        panel.add(errorButton, cc.xy(10, 19));
        errorButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             OptionPane.showErrorMessage(panel, "Error Message!", title,
                                         OptionPane.ABORT_RETRY_IGNORE_OPTIONS);
           }
         });

    return new ScrollPane(panel);
  }

// Inner Class. ----------------------------------------------------------------

  /**
   * Panel Label.
   */
  static class PanelLabel extends Label {
    public PanelLabel() {
      this(" ");
    }
    public PanelLabel(String text) {
      super(text);
      setBorderType(Label.BORDER_TYPE_STYLED);
      setBorderStyle(Label.BORDER_STYLE_SUNKEN);
    }
    public void setText(String text) {
      if (text == null) {
        super.setText(" ");
      } else {
        super.setText(text);
      }
    }
    public void setBackground(Color background) {
      if (background != null) {
        super.setBackground(background);
      }
    }
    public void setFont(Font font) {
      if (font != null) {
        super.setFont(font);
      }
    }
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new DialogsDemo().show();
  }

}
